package request

import (
	"strings"

	"github.com/gin-gonic/gin"
	"lab.com/define/errorcode"
)

// PostAddReq 论坛帖子新增参数
type PostAddReq struct {
	Title    string `json:"title" form:"title" validate:"required,max=200" comment:"标题"`
	AuthorID int    `json:"author_id" form:"author_id" validate:"required" comment:"发帖人ID"`
	Content  string `json:"content" form:"content" validate:"required" comment:"内容"`
}

// CheckInputParams return input params
func (post *PostAddReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(post); err != nil {
		return err
	}
	if errs, err := Validate(post); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	return nil
}

// PostEditReq 论坛帖子编辑参数
type PostEditReq struct {
	PostID  int    `json:"post_id" form:"post_id" validate:"required" comment:"帖子id"`
	Title   string `json:"title" form:"title" validate:"required,max=200" comment:"标题"`
	Content string `json:"content" form:"content" validate:"required" comment:"内容"`
}

// CheckInputParams return input params
func (post *PostEditReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(post); err != nil {
		return err
	}
	if errs, err := Validate(post); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	return nil
}

// PostListReq 论坛帖子列表
type PostListReq struct {
	Title string `json:"title" form:"title" comment:"帖子标题"`
	PageOffset
}

// CheckInputParams return input params
func (post *PostListReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(post); err != nil {
		return err
	}
	if errs, err := Validate(post); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	post.ParseOffset()
	return nil
}

// PostBasicInfo 帖子基本信息
type PostBasicInfo struct {
	PostID     int    `json:"post_id"`
	Title      string `json:"title" `
	Content    string `json:"content" `
	ReplyNum   int    `json:"reply_num"`
	UpdateTime string `json:"update_time"`
	AuthorID   int    `json:"author_id"`
	AuthorName string `json:"author_name"`
}

// PostListrRes 论坛帖子列表返回
type PostListRes struct {
	List  []PostBasicInfo `json:"list"`
	Count int             `json:"count"`
}

// PostDetailReq 论坛帖子详情
type PostDetailReq struct {
	PostID int `json:"post_id" form:"post_id" validate:"required" comment:"帖子id"`
}

// CheckInputParams return input params
func (post *PostDetailReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(post); err != nil {
		return err
	}
	if errs, err := Validate(post); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}
	return nil
}

// PostDetailRes 帖子详情返回
type PostDetailRes struct {
	PostID     int    `json:"post_id"`
	Title      string `json:"title" `
	Content    string `json:"content" `
	ReplyNum   int    `json:"reply_num"`
	CreateTime string `json:"create_time"`
	UpdateTime string `json:"update_time"`
	AuthorID   int    `json:"author_id"`
	AuthorName string `json:"author_name"`
}

// PostDeleteReq 论坛帖子删除参数
type PostDeleteReq struct {
	PostID int `json:"post_id" form:"post_id" validate:"required" comment:"帖子id"`
}

// CheckInputParams return input params
func (post *PostDeleteReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(post); err != nil {
		return err
	}
	if errs, err := Validate(post); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}
	return nil
}

// CommentAddReq 评论新增参数
type CommentAddReq struct {
	AuthorID   int    `json:"author_id" form:"author_id" validate:"required" comment:"评论人ID"`
	Content    string `json:"content" form:"content" validate:"required" comment:"内容"`
	PostID     int    `json:"post_id" form:"post_id" validate:"required" comment:"帖子id"`
	CommentPid int    `json:"comment_pid" form:"comment_pid" comment:"父级评论id"`
	TopPid     int    `json:"top_pid" form:"top_pid" comment:"顶级评论id"`
}

// CheckInputParams return input params
func (comment *CommentAddReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(comment); err != nil {
		return err
	}
	if errs, err := Validate(comment); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	return nil
}

// CommentListReq 帖子评论列表
type CommentListReq struct {
	PostID int `json:"post_id" form:"post_id" validate:"required" comment:"帖子ID"`
	PageOffset
}

// CheckInputParams return input params
func (comment *CommentListReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(comment); err != nil {
		return err
	}
	if errs, err := Validate(comment); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	comment.ParseOffset()
	return nil
}

// CommentBasicInfo 评论基本信息
type CommentBasicInfo struct {
	PostID        int    `json:"post_id" `
	CommentID     int    `json:"comment_id"`
	Content       string `json:"content" `
	CreateTime    string `json:"create_time"`
	AuthorID      int    `json:"author_id"`
	AuthorName    string `json:"author_name"`
	SubCommentNum int    `json:"sub_comment_num"`
}

// CommentListrRes 帖子评论列表返回
type CommentListRes struct {
	List  []CommentBasicInfo `json:"list"`
	Count int                `json:"count"`
}

// CommentUnfoldedListReq 帖子评论列表
type CommentUnfoldedListReq struct {
	PostID int `json:"post_id" form:"post_id" validate:"required" comment:"帖子ID"`
	PageOffset
}

// CheckInputParams return input params
func (comment *CommentUnfoldedListReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(comment); err != nil {
		return err
	}
	if errs, err := Validate(comment); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	comment.ParseOffset()
	return nil
}

// CommentUnfoldedBasicInfo 评论基本信息
type CommentUnfoldedBasicInfo struct {
	PostID          int    `json:"post_id" `
	CommentID       int    `json:"comment_id"`
	Content         string `json:"content" `
	CreateTime      string `json:"create_time"`
	AuthorID        int    `json:"author_id"`
	AuthorName      string `json:"author_name"`
	SubCommentNum   int    `json:"sub_comment_num"`
	ReplyAuthorID   int    `json:"reply_author_id"`
	ReplyAuthorName string `json:"reply_author_name"`
}

// CommentUnfoldedListrRes 帖子评论列表返回
type CommentUnfoldedListRes struct {
	List  []CommentUnfoldedBasicInfo `json:"list"`
	Count int                        `json:"count"`
}

// CommentInnerListReq 评论区内部列表
type CommentInnerListReq struct {
	CommentID int `json:"comment_id" form:"comment_id" validate:"required" comment:"评论ID"`
	PageOffset
}

// CheckInputParams return input params
func (comment *CommentInnerListReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(comment); err != nil {
		return err
	}
	if errs, err := Validate(comment); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	comment.ParseOffset()
	return nil
}